package com.zr.rabbitmq.controller;

import com.zr.rabbitmq.dto.MessageDTO;
import com.zr.rabbitmq.producer.MessageProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 消息发送控制器，提供API接口
 */
@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageProducer messageProducer;

    /**
     * 发送短信消息
     */
    @PostMapping("/sms")
    public ResponseEntity<String> sendSms(@RequestBody MessageDTO message) {
        message.setType("SMS");
        message.setSendTime(LocalDateTime.now());
        messageProducer.sendSmsMessage(message);
        return ResponseEntity.ok("短信消息已发送");
    }

    /**
     * 发送邮件消息
     */
    @PostMapping("/email")
    public ResponseEntity<String> sendEmail(@RequestBody MessageDTO message) {
        message.setType("EMAIL");
        message.setSendTime(LocalDateTime.now());
        messageProducer.sendEmailMessage(message);
        return ResponseEntity.ok("邮件消息已发送");
    }

    /**
     * 发送通知消息
     */
    @PostMapping("/notice")
    public ResponseEntity<String> sendNotice(
            @RequestBody MessageDTO message,
            @RequestParam String routingKey) {
        
        message.setType("NOTICE");
        message.setSendTime(LocalDateTime.now());
        messageProducer.sendNoticeMessage(message, routingKey);
        return ResponseEntity.ok("通知消息已发送，路由键: " + routingKey);
    }
}
